layui.use(['table', 'admin', 'ax', 'func','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;

    //日期范围
    laydate.render({
        elem: '#searchTime'
        ,type: 'date'
        ,range: '~'
        ,format: 'yyyy-MM-dd'
    });


    /**
     * 会员提现记录管理
     */
    var MemberWithdraw = {
        tableId: "memberWithdrawTable"
    };

    /**
     * 初始化表格的列
     */
    MemberWithdraw.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: 'center', hide: true, title: 'ID'},
            // {field: 'memberId',align: 'center', sort: true, title: '会员ID'},
            {field: 'memberName', align: 'center', sort: true, title: '用户名称'},
            {field: 'memberPhone', align: 'center', sort: true, title: '用户手机'},
            {field: 'money', align: 'center', sort: true, title: '提现金额'},
            // {field: 'lastBalance', align: 'center',sort: true, title: '提现前余额'},
            // {field: 'balance', align: 'center',sort: true, title: '提现后余额'},
            {
                field: 'accountType', align: 'center', sort: true, title: '账户类型', templet: function (d) {
                    var flag_box = "";
                    if (d.accountType === 0) {
                        flag_box = "支付宝";
                    } else if (d.accountType === 1) {
                        flag_box = "微信";
                    } else if (d.accountType === 2) {
                        flag_box = "银联";
                    }
                    return flag_box;
                }
            },
            {field: 'withdrawAccount', align: 'center', sort: true, title: '提现账号'},
            {field: 'withdrawName', align: 'center', sort: true, title: '实名信息'},
            // {field: 'bankName', align: 'center', sort: true, hide: true, title: '银行名称'},
            // {field: 'bankBranchName', align: 'center', sort: true, hide: true, title: '支行信息'},
            {
                field: 'status', align: 'center', sort: true, title: '提现状态', templet: function (d) {
                    var flag_box = "";
                    if (d.status === 0) {
                        flag_box = "<span class='layui-btn layui-btn-xs layui-btn-danger' >待审核</span>";
                    } else if (d.status === 2) {
                        flag_box = "<span class=\"layui-btn layui-btn-xs layui-btn-disabled \">审核失败</span>";
                    } else if (d.status === 1) {
                        flag_box = "<span class=\"layui-btn layui-btn-xs \">审核通过</span>";
                    }
                    return flag_box;
                }
            },
            {field: 'reason', align: 'center', sort: true, title: '拒绝原因'},
            // {field: 'createBy', align: 'center', sort: true, hide: true, title: '创建者'},
            {field: 'createTime', align: 'center', sort: true, title: '提交时间'},
            // {field: 'updateBy', align: 'center', sort: true, hide: true, title: '更新者'},
            {field: 'updateTime', align: 'center', sort: true, title: '审核时间'},
            // {field: 'remark', align: 'center', sort: true, hide: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    MemberWithdraw.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#condition").val();
        queryData['searchTime'] = $("#searchTime").val();
        table.reload(MemberWithdraw.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    /**
     * 弹出添加对话框
     */
    MemberWithdraw.openAddDlg = function () {
        func.open({
            title: '添加会员提现记录',
            content: Feng.ctxPath + '/memberWithdraw/add',
            tableId: MemberWithdraw.tableId
        });
    };

    /**
     * 点击同意
     *
     * @param data 点击按钮时候的行数据
     */
    MemberWithdraw.openEditDlg = function (data) {
        var operation = function () {
            data.status = 1;
            var ajax = new $ax(Feng.ctxPath + "/memberWithdraw/withdrawAuth", function (data) {
                Feng.success("操作成功！");
                table.reload(MemberWithdraw.tableId);
            }, function (data) {
                Feng.error("更新失败！" + data.responseJSON.message)
            });
            ajax.set(data);
            ajax.start();
        };
        Feng.confirm("是否通过?", operation);
    };

    /**
     * 点击拒绝
     *
     * @param data 点击按钮时候的行数据
     */
    MemberWithdraw.onDeleteItem = function (data) {
        layer.prompt({title: '请填写拒绝原因', formType: 2}, function (text, index) {
            console.log(text);
            console.log(data);
            data.reason = text;
            data.status = 2;
            layer.close(index);
            var ajax = new $ax(Feng.ctxPath + "/memberWithdraw/withdrawAuth", function (data) {
                Feng.success("操作成功！");
                table.reload(MemberWithdraw.tableId);
            }, function (data) {
                Feng.error("更新失败！" + data.responseJSON.message)
            });
            ajax.set(data);
            ajax.start();
        });
    };


    /**
     * 导出excel按钮
     */
    MemberWithdraw.exportExcel = function () {
        var checkRows = table.checkStatus(MemberWithdraw.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + MemberWithdraw.tableId,
        url: Feng.ctxPath + '/memberWithdraw/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: MemberWithdraw.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        MemberWithdraw.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        MemberWithdraw.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        MemberWithdraw.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + MemberWithdraw.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            MemberWithdraw.openEditDlg(data);
        } else if (layEvent === 'delete') {
            MemberWithdraw.onDeleteItem(data);
        }
    });
});
